package com.wu.dms.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.convertion.errorcode.SongListErrorCode;
import com.wu.common.convertion.exception.ServiceException;
import com.wu.constant.Enum.MinIoNameEnum;
import com.wu.dms.dao.entity.SongList;
import com.wu.dms.dao.mapper.SongListMapper;
import com.wu.dms.dto.SongListRequestDTO;
import com.wu.dms.service.SongListService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;




@Service
@RequiredArgsConstructor
public class SongListServiceImpl extends ServiceImpl<SongListMapper, SongList> implements SongListService {



    private final MinIoUtil minIoUtil;


    @Override
    public void updatePlayListMsg(SongListRequestDTO updateSongListRequestDTO) {
        LambdaUpdateWrapper<SongList> updateWrapper = Wrappers.lambdaUpdate(SongList.class)
                .set(SongList::getIntroduction, updateSongListRequestDTO.getIntroduction())
                .set(SongList::getStyle, updateSongListRequestDTO.getStyle())
                .set(SongList::getTitle, updateSongListRequestDTO.getTitle());
        if (!update(updateWrapper)) {
            throw new ServiceException(SongListErrorCode.SONG_LIST_UPDATE_ERROR);
        }
    }

    @Transactional
    @Override
    public void deletePlayList(Long id) {
        if (!removeById(id)) {
            throw new ServiceException(SongListErrorCode.SONG_LIST_DELETE_ERROR);
        }

    }


    @Override
    public List<SongList> likeTitle(String title) {

        return query().like("title", title).list();
    }

    @Override
    public List<SongList> likeStyle(String style) {


        return query().like("style", style).list();
    }

    @Transactional

    @Override
    public void addPlayList(SongListRequestDTO addSongListRequestDTO) {
        SongList songList = new SongList();
        BeanUtils.copyProperties(addSongListRequestDTO, songList);
        songList.setPic(MinIoNameEnum.SONG_LIST_IMAGE.getDefaultObjectName());
        if (!save(songList)) {
            throw new ServiceException(SongListErrorCode.SONG_LIST_SAVE_ERROR);
        }
    }


    @SneakyThrows
    @Override
    public void updatePlayListImg(MultipartFile avatarFile, @RequestParam("id") Long id) {

        String fileName = avatarFile.getOriginalFilename();
        if (MinIoNameEnum.SONG_LIST_IMAGE.getDefaultObjectName().equals(fileName)){
            return;
        }
        SongList songList = query().eq("id", id).one();
        if (songList == null) {
            return;
        }
        String originImageFileName = songList.getPic();
        minIoUtil.deleteFile(MinIoNameEnum.SONG_LIST_IMAGE.getBucketName(), MinIoNameEnum.getDefaultObjectName(id,originImageFileName));

        minIoUtil.uploadFile(MinIoNameEnum.SONG_LIST_IMAGE.getBucketName(), MinIoNameEnum.getDefaultObjectName(id,fileName), avatarFile.getInputStream(), avatarFile.getSize());
        LambdaUpdateWrapper<SongList> updateWrapper = Wrappers.lambdaUpdate(SongList.class)
                .eq(SongList::getId, id)
                .set(SongList::getPic, fileName);
        update(updateWrapper);

    }
}